#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2022/2/23 21:01
# @Author : Aries
# @Site :
# @File : steam.py
# @Software: PyCharm

import execjs
import time
import requests
import tkinter as tk



class SteamLogin():

    def __init__(self, username, password):
        self.username = username
        self.password = password
        self.session = requests.session()
        self.session.headers = {
            'Host': 'steamcommunity.com',
            'Origin': 'https://steamcommunity.com',
            'Referer': 'https://steamcommunity.com/login/home/?goto=',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36',
        }

    # 获取时间戳
    def get_timestamp(self):
        timestamp = int(time.time() * 1000)
        timestamp = str(timestamp)
        return timestamp

    def get_mod_and_exp(self):
        url = 'https://steamcommunity.com/login/getrsakey/'
        data = {
            'donotcache': self.get_timestamp(),
            'username': self.username,
        }
        response = self.session.post(url,data=data)
        if response.status_code == 200:
            info = response.json()
            return info

    def get_pwd(self):
        node = execjs.get()
        etx = node.compile(open('steam_index.js',encoding='utf-8').read())
        functionname = "getpwd('{}','{}','{}')".format(self.password,self.get_mod_and_exp()['publickey_mod'],self.get_mod_and_exp()['publickey_exp'])
        result = etx.eval(functionname)
        return result

    def parse_login(self):
        url = 'https://steamcommunity.com/login/dologin/'
        rsatimestamp = self.get_mod_and_exp()['timestamp']
        # print(rsatimestamp)
        data = {
            'donotcache': self.get_timestamp(),
            'password': self.get_pwd(),
            'username': self.username,
            'twofactorcode': '',
            'emailauth': '',
            'loginfriendlyname': '',
            'captchagid':'-1',
             'captcha_text': '',
            'emailsteamid': '',
            'rsatimestamp': rsatimestamp,
            'remember_login': 'false',
            'tokentype': '-1',
        }

        response = self.session.post(url,data=data)
        if response.status_code == 200:
            print(response.json())
            self.text_obj.insert('insert',response.json())
            data = response.json()
            if 'emailauth_needed' not in data.keys():
                return False
            else:
                return True
    def visual_interface(self):
        window = tk.Tk()
        window.title('steam登录可视化窗口')
        window.geometry('500x300')

        lable_obj = tk.Label(window, text="点击登录按钮进行登录",width=20, height=1, fg='#000', bg='yellow')
        lable_obj.pack()

        button_obj = tk.Button(window, text='登录', width=10, height=1, font=('Arial',12),command=self.main)
        button_obj.pack()

        self.text_obj = tk.Text(window,height=20)
        self.text_obj.pack()

        window.mainloop()

    def main(self):
        login_bool = self.parse_login()
        print(login_bool)
        if login_bool == False:
            print('登录失败')
        else:
            emailauth = input("请输入你的验证码:")
            self.text_obj.insert('insert',emailauth)
            url = 'https://steamcommunity.com/login/dologin/'
            rsatimestamp = self.get_mod_and_exp()['timestamp']
            data = {
                'donotcache': self.get_timestamp(),
                'password': self.get_pwd(),
                'username': self.username,
                'twofactorcode': '',
                'emailauth': emailauth,
                'loginfriendlyname': '',
                'captchagid': '-1',
                'captcha_text': '',
                'emailsteamid': '',
                'rsatimestamp': rsatimestamp,
                'remember_login': 'false',
                'tokentype': '-1',
            }
            response = self.session.post(url,data=data)
            print(response.json())
            self.text_obj.insert('insert', response.json())
            if response.json()['success'] == True:
                print("已经登陆成功")
            else:
                print("登陆失败")



if __name__ == '__main__':
    steamLogin = SteamLogin('2890875416','soloman3210')
    steamLogin.visual_interface()